import { wrong_reg, for_obj, ascii_print, is_empty, loc_hm, loc_time, loc_day } from '../../nui';
import { loc } from '../../local';

const wrong_col = (item, val) => {
    const t = val.split(",");
    for (let i = 0; i < t.length; i++) {
        const d = t[i];
        let v = item[d];
        if (!v) {
            let tmp = "";
            for_obj(item, (k, v) => tmp += `${v} `);
            return `${tmp} , ${loc("gscw")}: ${d}, ${loc("w0")}: ${val}`;
        }
    }

    return null;
};

const wrong_name = (data) => {
    const no = ['*', '\\', '[', ']', '/'];

    for (let i = 0; i < data.length; i++) {
        const c = data.charAt(i);
        if (c === ' ') {
            return `${loc("w1")}: ${data}`;
        }

        for (let j = 0; j < no.length; j++) {
            if (c === no[j]) {
                return `${loc("w2")}${c}: ${data}`;
            }
        }
    }

    return null;
};

const wrong_num = (data) => wrong_reg(data, /^[0-9]+$/, loc("w3"));

const wrong_num_cmp = (data, com, info, pre="") => {
    let ret = wrong_num(data);
    if (ret) {
        return ret;
    }

    ret = parseInt(data, 10);
    if (ret < com) {
        return `${pre}${info}${loc("w4")}${com}`;
    }

    return null;
};

const wrong_item = (item, pre="") => {
    if (item.name) {
        const ret = wrong_name(item.name);
        if (ret) {
            return pre + ret;
        }
    }

    if (item.sn) {
        const ret = ascii_print(item.sn);
        if (ret) {
            return pre + ret;
        }
    }

    return null;
};

// YYYY-MM-DDTHH:MM:SS
const wrong_time = (data) => {
    const err = `${loc("w5")}: ${data}, ${loc("w6")}: YYYY-MM-DDTHH:MM:SS`;

    const v = new Date(data);
    if (!v) {
        return err;
    }

    const tmp = loc_day(v) + "T" + loc_time(v);
    if (tmp !== data) {
        return err;
    }

    return null;
};

// HH:MM
const wrong_hm = (data) => {
    const err = `${loc("w5")}: ${data}, ${loc("w6")}: HH:MM`;

    const v = new Date(data);
    if (!v) {
        return err;
    }

    const tmp = loc_hm(v);
    if (tmp !== data) {
        return err;
    }

    return null;
};

/*
 * ck: {
 *     col: "sn,name,tcode,num,pos",
 *     noitem: true,
 *     cnum: [
 *         {k: "pos", v: 1, s: "起始位置"}
 *     ],
 *     num: ["num"]
 * }
*/
const wrong_check = (item, pre, ck) => {
    if (ck.col) {
        const ret = wrong_col(item, ck.col);
        if (ret) {
            return ret;
        }
    }

    if (!ck.noitem) {
        const ret = wrong_item(item, pre + item.sn);
        if (ret) {
            return ret;
        }
    }

    if (!is_empty(ck.num)) {
        for (let i = 0; i < ck.num.length; i++) {
            const ret = wrong_num(item[ck.num[i]]);
            if (ret) {
                return ret;
            }
        }
    }

    if (!is_empty(ck.cnum)) {
        for (let i = 0; i < ck.cnum.length; i++) {
            const d = ck.cnum[i];
            const ret = wrong_num_cmp(item[d.k], d.v, d.s, pre);
            if (ret) {
                return ret;
            }
        }
    }

    if (ck.hm) {
        const ret = wrong_hm(item[item.hm]);
        if (!ret) {
            return ret;
        }
    }

    if (!is_empty(ck.time)) {
        for (let i = 0; i < ck.time.length; i++) {
            const ret = wrong_time(item[ck.time[i]]);
            if (ret) {
                return ret;
            }
        }
    }

    return null;
};

export {wrong_check};
